# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=markdown
_pyname=Markdown
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.1.1
pkgrel=1
pkgdesc="Python implementation of John Gruber's Markdown (mingw-w64)"
url='https://pypi.python.org/pypi/Markdown'
license=('BSD')
arch=('any')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-pbr"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-yaml")
install=python3-${_realname}-${CARCH}.install
source=("https://pypi.io/packages/source/${_pyname:0:1}/${_pyname}/${_pyname}-${pkgver}.tar.gz")
sha256sums=('2e50876bcdd74517e7b71f3e7a76102050edec255b3983403f1a63e7c8a41e7a')

prepare() {
  cd "${srcdir}"

  # bug in 2.4, some DOS line endings slipped in
  find "${_realname}-${pkgver}" -name '*py' -exec sed -i 's|\r||g' {} +

  # ImportError: No module named pkg_resources
  sed -i 's/^from pkg_resources.*$/import packaging.version/' "${_realname}-${pkgver}/markdown/__init__.py"

  cd "${srcdir}"
  rm -rf "python-build-${CARCH}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python -m unittest discover tests
}

package() {
  cd "${srcdir}/python-build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --skip-build

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|$(cygpath -wm ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${_f}"
  done

  install -D -m644 LICENSE.md "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE.md
}
